home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 April / EnigmA AMIGA RUN 17 (1997)(G.R. Edizioni)(IT)[!][issue 1997-04][EAR-CD].iso / EARCD / text / hyper / hsc_source.lha / hsc / source / hsclib / attrib.c < prev    next >
C/C++ Source or Header  |  1996-12-04  |  9KB  |  482 lines

  1. /*
  2.  * hsclib/attribute.c
  3.  *
  4.  * hsc-variable funcs for hsc
  5.  *
  6.  * Copyright (C) 1995,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated:  4-Dec-1996
  23.  * created:  2-Sep-1995
  24.  *
  25.  */
  26.  
  27. #define NOEXTERN_HSCLIB_ATTRIBUTE_H
  28.  
  29. #include "hsclib/inc_base.h"
  30.  
  31. #include "hsclib/eval.h"
  32. #include "hsclib/uri.h"
  33.  
  34. /*
  35.  *-------------------------------------
  36.  * debugging functions
  37.  *-------------------------------------
  38.  */
  39.  
  40. /*
  41.  * ptr_var
  42.  */
  43. static VOID prt_var(FILE * stream, APTR data)
  44. {
  45.     HSCATTR *var = (HSCATTR *) data;
  46.  
  47.     if (var)
  48.     {
  49.         int varquote = var->quote;
  50.         if (varquote == VQ_NO_QUOTE)
  51.             varquote = '.';
  52.  
  53.         /* name & type & macro_id */
  54.         fprintf(stream, "%s (type:%u,mci:%lu) ",
  55.                 var->name, var->vartype, var->macro_id);
  56.         /* text value */
  57.         if (var->text)
  58.             fprintf(stream, "cur:%c%s%c",
  59.                     var->quote, var->text, varquote);
  60.         else
  61.             fprintf(stream, "<NULL>");
  62.         fprintf(stream, " ");
  63.         /* default text */
  64.         if (var->deftext)
  65.             fprintf(stream, "def:%c%s%c",
  66.                     var->quote, var->deftext, varquote);
  67.         else
  68.             fprintf(stream, "<NULL>");
  69.         fprintf(stream, "\n");
  70.     }
  71.     else
  72.         fprintf(stream, "<NULL>\n");
  73. }
  74.  
  75. VOID prt_varlist(DLLIST * varlist, STRPTR title)
  76. {
  77.     fprintf(stderr, DHL "%s\n", title);
  78.     fprintf_dllist(stderr, varlist, prt_var);
  79. }
  80.  
  81. /*
  82.  *-------------------------------------
  83.  * constructor/destructor
  84.  *-------------------------------------
  85.  */
  86.  
  87. /*
  88.  * del_hscattr
  89.  *
  90.  * delete attribute
  91.  */
  92. VOID del_hscattr(APTR data)
  93. {
  94.     HSCATTR *var = (HSCATTR *) data;
  95.  
  96. #if DEBUG_ATTR
  97.     fprintf(stderr, DHL "   del_attr %s (mci=%d)\n",
  98.             var->name, var->macro_id);
  99. #endif
  100.  
  101.     /* release mem */
  102.     ufreestr(var->name);
  103.     ufreestr(var->deftext);
  104.     ufreestr(var->text);
  105.     ufreestr(var->enumstr);
  106.  
  107.     var->macro_id = 0;
  108.     var->varflag = 0;
  109.     var->vartype = VT_NONE;
  110.     var->quote = EOF;
  111.  
  112.     ufree(var);
  113.  
  114. }
  115.  
  116. /*
  117.  * new_hscattr
  118.  *
  119.  * alloc & init a new variable
  120.  */
  121. HSCATTR *new_hscattr(STRPTR newname)
  122. {
  123.     HSCATTR *newvar = (HSCATTR *) umalloc(sizeof(HSCATTR));
  124.  
  125. #if DEBUG_ATTR
  126.     fprintf(stderr, DHL "   new_attr %s\n", newname);
  127. #endif
  128.  
  129.     if (newvar)
  130.     {
  131.         /* init new varument item */
  132.         newvar->vartype = VT_NONE;
  133.         newvar->name = strclone(newname);
  134.         newvar->deftext = NULL;
  135.         newvar->text = NULL;
  136.         newvar->enumstr = NULL;
  137.         newvar->macro_id = 0;
  138.         if (upstrncmp(newname, PREFIX_HSCATTR, strlen(PREFIX_HSCATTR)))
  139.             newvar->varflag = 0;
  140.         else
  141.             newvar->varflag = VF_KEEP_QUOTES;
  142.         newvar->quote = VQ_NO_QUOTE;
  143.     }
  144.  
  145.     if (!(newvar->name))
  146.     {
  147.         del_hscattr((APTR) newvar);
  148.         newvar = NULL;
  149.     }
  150.  
  151.     return (newvar);
  152. }
  153.  
  154. /*
  155.  * cpy_hscattr
  156.  *
  157.  * create copy of an already existing attribute
  158.  */
  159. HSCATTR *cpy_hscattr(HSCATTR * oldvar)
  160. {
  161.     HSCATTR *newvar = new_hscattr(oldvar->name);
  162.  
  163.     if (newvar)
  164.     {
  165.         newvar->vartype = oldvar->vartype;
  166.         if (oldvar->deftext)
  167.             newvar->deftext = strclone(oldvar->deftext);
  168.         if (oldvar->text)
  169.             newvar->text = strclone(oldvar->text);
  170.         if (oldvar->enumstr)
  171.             newvar->enumstr = strclone(oldvar->enumstr);
  172.         newvar->macro_id = oldvar->macro_id;
  173.         newvar->varflag = oldvar->varflag;
  174.         newvar->quote = oldvar->quote;
  175.     }
  176.  
  177.     if (!(newvar->name))
  178.     {
  179.         del_hscattr((APTR) newvar);
  180.         newvar = NULL;
  181.     }
  182.  
  183.     return (newvar);
  184.  
  185. }
  186.  
  187. /*
  188.  * app_var
  189.  *
  190.  * append a new var to the var list AT BEGINNING
  191.  */
  192. HSCATTR *app_var(DLLIST * varlist, STRPTR newname)
  193. {
  194.     HSCATTR *var = new_hscattr(newname);
  195.     BOOL ok = FALSE;
  196.  
  197.     if (var)
  198.         ok = (ins_dlnode(varlist, varlist->first, (APTR) var) != NULL);
  199.  
  200.     if (!ok)
  201.     {
  202.         del_hscattr((APTR) var);
  203.         var = NULL;
  204.     }
  205.  
  206.     return (var);
  207. }
  208.  
  209. /*
  210.  *-------------------------------------
  211.  * search functions
  212.  *-------------------------------------
  213.  */
  214.  
  215. /*
  216.  * cmp_varname
  217.  *
  218.  * compares a var-name with the name
  219.  * of a HSCATTR-entry
  220.  */
  221. int cmp_varname(APTR cmpstr, APTR vardata)
  222. {
  223.     STRPTR varstr = NULL;
  224.  
  225.     if (vardata)
  226.         varstr = ((HSCATTR *) vardata)->name;
  227.  
  228.     if (varstr)
  229.         if (!upstrcmp(cmpstr, varstr))
  230.             return (-1);
  231.         else
  232.             return (0);
  233.     else
  234.         return (0);
  235. }
  236.  
  237. /*
  238.  * find_attrnode
  239.  */
  240. DLNODE *find_attrnode(DLLIST * varlist, STRPTR name)
  241. {
  242.     DLNODE *nd = find_dlnode(varlist->first, (APTR) name, cmp_varname);
  243.  
  244.     return (nd);
  245. }
  246.  
  247. /*
  248.  * find_varname
  249.  */
  250. HSCATTR *find_varname(DLLIST * varlist, STRPTR name)
  251. {
  252.     DLNODE *nd = find_dlnode(varlist->first, (APTR) name, cmp_varname);
  253.     HSCATTR *var = NULL;
  254.  
  255.     if (nd)
  256.         var = (HSCATTR *) nd->data;
  257.  
  258.     return (var);
  259. }
  260.  
  261. /*
  262.  *-------------------------------------
  263.  * misc. functions
  264.  *-------------------------------------
  265.  */
  266.  
  267. /*
  268.  * set_vartext
  269.  *
  270.  * set value of a var
  271.  *
  272.  * params: var......var to set
  273.  *         newtext..new text to set
  274.  * result: value of new text set
  275.  * errors: return NULL if out of mem, display error message
  276.  */
  277. STRPTR set_vartext(HSCATTR * var, STRPTR newtext)
  278. {
  279.     if (newtext != var->text)
  280.     {
  281.         ufreestr(var->text);
  282.         var->text = NULL;
  283.         if (newtext)
  284.             var->text = strclone(newtext);
  285.     }
  286.  
  287.     return (var->text);
  288. }
  289.  
  290. /*
  291.  * set_varbool
  292.  *
  293.  * set value of a boolean attr
  294.  *
  295.  * params: var....var to set
  296.  *         value..new value to set
  297.  * result: value
  298.  * errors: if out of mem, display error message
  299.  */
  300. BOOL set_varbool(HSCATTR * attr, BOOL value)
  301. {
  302.     if (value)
  303.         set_vartext(attr, attr->name);
  304.     else
  305.         set_vartext(attr, "");
  306.  
  307.     return (value);
  308. }
  309.  
  310. /*
  311.  * clr_vartext
  312.  *
  313.  * clear or reset var to default text
  314.  *
  315.  * params: var..var to clear
  316.  * result: TRUE, if new value set correctly
  317.  */
  318. BOOL clr_vartext(HSCATTR * var)
  319. {
  320.     BOOL ok = TRUE;
  321.  
  322.     if (var->deftext)
  323.     {
  324.         if (!(set_vartext(var, var->deftext)))
  325.             ok = FALSE;
  326.     }
  327.     else
  328.     {
  329.         ufreestr(var->text);
  330.         var->text = NULL;
  331.     }
  332.  
  333.     return (ok);
  334. }
  335.  
  336. /*
  337.  * clr_attrdef
  338.  *
  339.  * clear attributes default text
  340.  *
  341.  * params: attr..attr to clear
  342.  */
  343. VOID clr_attrdef(HSCATTR * attr)
  344. {
  345.     ufreestr(attr->deftext);
  346.     attr->deftext = NULL;
  347. }
  348.  
  349. /*
  350.  * clr_varlist
  351.  *
  352.  * clear all vars to default text
  353.  *
  354.  * params: varlist..varlist to clear
  355.  */
  356. BOOL clr_varlist(DLLIST * varlist)
  357. {
  358.     DLNODE *nd = varlist->first;
  359.     BOOL ok = TRUE;
  360.  
  361.     while (nd && ok)
  362.     {
  363.         ok &= clr_vartext((HSCATTR *) nd->data);
  364.         nd = nd->next;
  365.     }
  366.  
  367.     return (ok);
  368. }
  369.  
  370. /*
  371.  * clr_varlist_bool
  372.  *
  373.  * set all "empty" (=NULL) boolean attributes of
  374.  * an attr list to FALSE
  375.  *
  376.  * params: varlist..varlist to process
  377.  */
  378. VOID clr_varlist_bool(DLLIST * varlist)
  379. {
  380.     DLNODE *nd = varlist->first;
  381.     BOOL ok = TRUE;
  382.  
  383.     while (nd && ok)
  384.     {
  385.         HSCATTR *attr = (HSCATTR *) nd->data;
  386.  
  387.         if ((attr->vartype == VT_BOOL) && !(attr->text))
  388.             set_varbool(attr, FALSE);
  389.  
  390.         nd = nd->next;
  391.     }
  392. }
  393.  
  394. /*
  395.  * get_vartext_byname: get text value of a var
  396.  *
  397.  *
  398.  */
  399. STRPTR get_vartext_byname(DLLIST * varlist, STRPTR name)
  400. {
  401.     HSCATTR *var = find_varname(varlist, name);
  402.     STRPTR vartext = NULL;
  403.  
  404.     if (var)
  405.         vartext = var->text;
  406.  
  407.     return (vartext);
  408. }
  409.  
  410. /*
  411.  * get_vartext: get text value of a var
  412.  *
  413.  */
  414. STRPTR get_vartext(HSCATTR * var)
  415. {
  416.     STRPTR text = NULL;
  417.  
  418.     if (var)
  419.         text = var->text;
  420.  
  421.     return (text);
  422. }
  423.  
  424. /*
  425.  * get_varbool: get value of a boolean attr
  426.  */
  427. BOOL get_varbool(HSCATTR * attr)
  428. {
  429.     BOOL set = FALSE;
  430.     if (attr && (attr->text[0]))
  431.         set = TRUE;
  432.  
  433.     return (set);
  434. }
  435.  
  436. /*
  437.  * get_varbool_byname: get value of a boolean attr
  438.  */
  439. BOOL get_varbool_byname(DLLIST * varlist, STRPTR name)
  440. {
  441.     HSCATTR *var = find_varname(varlist, name);
  442.  
  443.     return (get_varbool(var));
  444. }
  445.  
  446. /*
  447.  * get_varnum: get value of a numeric attr
  448.  */
  449. LONG get_varnum(HSCATTR * attr)
  450. {
  451.     LONG num = 0;
  452.     if (attr && (attr->text) && (attr->text[0]))
  453.         str2long(attr->text, &num);
  454.  
  455.     return (num);
  456. }
  457.  
  458. /*
  459.  * get_varnum_byname: get value of a numeric attr
  460.  */
  461. LONG get_varnum_byname(DLLIST * varlist, STRPTR name)
  462. {
  463.     HSCATTR *var = find_varname(varlist, name);
  464.  
  465.     return (get_varnum(var));
  466. }
  467.  
  468. /*
  469.  * get_vardeftext: get default text value of a var
  470.  *
  471.  */
  472. STRPTR get_vardeftext(HSCATTR * var)
  473. {
  474.     STRPTR deftext = NULL;
  475.  
  476.     if (var)
  477.         deftext = var->deftext;
  478.  
  479.     return (deftext);
  480. }
  481.  
  482.